package base.flowGallery {
	import com.greensock.TweenMax;
	import flash.display.Sprite;
	
	public class FlowGallery extends Sprite {
		
		private var _t: Number;
		private var listItems: ListItems;

		private var DIFF							:Number = 0.029
		private var  NUM							:Number = 0
		private const UP_LIMIT						:Number = .79
		private const DN_LIMIT						:Number = 0.58
		private const CENTER						:Number = (DN_LIMIT+UP_LIMIT)/2
		private const AIM							:Number = 0.65
		private const ALPHA_SPEED					:Number = 50
		private var HIDE_ZONE						:Number = 1/ALPHA_SPEED
		
		private var _cont							:Sprite = new Sprite()
			
		private var _timer							:Timer = new Timer(5000)
		
		
		private var _items							:Array = []
		private var _curr							:Leaf
		private var _currNum						:int = 0
	
		private var _mid							:int
		private var _wheel							:Boolean = true
		private var _auto 							:Boolean = false
		
	
		private var _data							:Object
		private var _speed							:Number ;
		
		private var _t: Number;
		private var currInd: uint;
		
		
		public function FlowGallery($listItems: ListItems): void {
			_listItems = $listItems;
			initItems();
		}
		
	
		private function itemSelected(e:Event):void {
			select(e.currentTarget)
			
		}
		
		private function select($item:Item):void {
			
			TweenMax.killTweensOf(_curr)
			
			_curr.selected = false
			_curr = $item
			_curr.selected = true
			
			if (_curr.t < AIM) {
				_speed = 1
			} else {
				_speed = -1
			}
			
			
			_cont.setChildIndex(_curr, _cont.numChildren - 1)
			TweenMax.to(this, 0.3 + Math.abs(_curr.pos - AIM) * 3, {_t: (currInd + _speed) * DIFF, onUpdate:update, ease:Quad.easeOut});
			dispatchEvent(new Event(Event.SELECT))
		}
		
		private function get t(): Number {
			return _t % 1;
		} 
		
		
		private function update(e:Event=null):void {
			fix()
			render()
			
		}
		
		
		private function fix():void {
			
			
			if (_speed>0) {
				
				_mid = Math.round(_items.length / 2);
				
				while (_items[_mid].t > CENTER) {
					_items[_items.length-1].t = _items[0].t-DIFF;
					_items.splice(0,0,_items[_items.length-1]);
					_items.pop();
					
					_mid = Math.round(_items.length / 2);
				}
			} else {
				
				_mid = Math.round(_items.length/2)-1;
				
				while (_items[_mid].t < CENTER) {
					
					_items[0].t = _items[_items.length-1].t+DIFF;
					_items.push(_items[0]);
					_items.splice(0, 1);
					
					_mid = Math.round(_items.length/2)-1;
				}
			}
		}
		
	
		
	
		private function render():void {
		
			for (var i:int = 0; i < _items.length; i++) {
				i / this.listItems
				
				
				if(_items[i].t >= DN_LIMIT-HIDE_ZONE && _items[i].t <= UP_LIMIT+HIDE_ZONE){
					if (!_cont.contains(_items[i])) {
						_cont.addChild(_items[i])
					}
					TweenMax.to(_items[i],0.3,{alpha:1})
					
					_cont.setChildIndex(_items[i], _cont.numChildren - 2)
					_cont.setChildIndex(_curr, _cont.numChildren - 1)
					
				} else {
					TweenMax.to(_items[i], 0.15, { alpha:0, onComplete:removeItem,onCompleteParams:[_items[i]]} )
				}
				_items[i].rotation = _items[i].t * 360
				/*if ( _items[i].t > UP_LIMIT ) {
					_items[i].alpha = 1-(_items[i].t-UP_LIMIT)*ALPHA_SPEED
				}else if ( _items[i].t  < DN_LIMIT) {
					_items[i].alpha = 1-(DN_LIMIT-_items[i].t)*ALPHA_SPEED
				}else if (_items[i].t > DN_LIMIT && _items[i].t < UP_LIMIT){
					_items[i].alpha = 1
				}*/
				
			}
		}
		
		private function removeItem($i:Leaf):void {
			if (_cont.contains($i))  { 
				_cont.removeChild($i)
			}
				
		}
		
		
		
		private function initEvents():void
		{
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler, false, 0, true)
			_timer.addEventListener(TimerEvent.TIMER, onTimer)
			dispatchEvent(new Event(Event.SELECT))
		}
		
		private function onTimer(e:TimerEvent):void 
		{
			_auto = true
		}
		
		private function mouseWheelHandler(e:MouseEvent):void 
		{
			if (_wheel) {
				_wheel = false
				if (e.delta > 0) {
					next()
				}else {
					prev()
				}
				setTimeout(function():void{_wheel = true},50)
			}
		}
				
		private function next(e:Event = null):void {
			//_timer.reset()
			//_timer.start()
			_auto = false
			select(_curr.n)
		}
		
		private function prev(e:Event = null):void {
			//_timer.reset()
			//_timer.start()
			_auto = false
			select(_curr.p)
		}
				
	
	
		
	}
}